package tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchFile {
	public static void main(String[] args) throws Exception {
		String directoryString = "/home/dsw/";
		String regexString = ".*add.*request.*";
		regexString = ".*title.*iPrint.*";
		regexString = "(.*)(\tnull\t)(.*)";
		System.out.println("doperator.propertiesf".matches(regexString));

		/* add pay */
		int count = 0;
		// searchFileName("/home/dsw/workspaces/workspace07", ".*category.*");
		count = searchFile(0, directoryString, regexString);
		System.out.println("共找到" + count + "个");
		System.out.println(new Date());
	}

	public static int search(int count, String fileString, String regexString) throws Exception {
		File file = new File(fileString);
		fileString = "/home/dsw/log/search.log";
		if (file.isDirectory()) {
			File subFiles[] = file.listFiles();
			for (File subFile : subFiles) {
				count = search(count, subFile.toString(), regexString);
			}
		} else {
			if(file.length()>10000000){
				return count;
			}
			if (!file.toString().endsWith("java111")) {
				FileInputStream fis = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);

				String str = null;
				int i = 0;
				boolean flag = true;
				while (null != (str = br.readLine())) {
					i++;
					Pattern pattern = Pattern.compile(regexString, Pattern.CASE_INSENSITIVE);
					Matcher matcher = pattern.matcher(str);
					if (matcher.matches()) {
						count++;
						if (flag) {
							System.out.println(file + "\r\n" + " : 第" + i + "行:\r\n" + str);
							save(file + "\r\n" + " : 第" + i + "行:\r\n" + str, fileString);
							flag = false;
						} else {
							System.out.println("第" + i + "行:\r\n" + str);
							save("第" + i + "行:\r\n" + str, fileString);
						}
					}
				}
				br.close();
				isr.close();
				fis.close();
			}
		}

		return count;
	}

	public static int searchFile(int count, String fileString, String regexString) throws Exception {
		File file = new File(fileString);
		fileString = "/home/dsw/log/search.log";
		if (file.isDirectory()) {
			File subFiles[] = file.listFiles();
			for (File subFile : subFiles) {
				count = searchFile(count, subFile.toString(), regexString);
			}
		} else {
			if(file.length()>10000000){
				return count;
			}
			if (!file.toString().endsWith("java111")) {
				FileInputStream fis = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);

				String str = null;
				int i = 0;
				boolean flag = true;
				while (null != (str = br.readLine())&&flag) {
					i++;
					Pattern pattern = Pattern.compile(regexString, Pattern.CASE_INSENSITIVE);
					Matcher matcher = pattern.matcher(str);
					if (matcher.matches()) {
						count++;
						flag = false;
						System.out.println(file + "\r\n");
						save(file + "\r\n" + " : 第" + i + "行:\r\n" + str, fileString);
					}
				}
				br.close();
				isr.close();
				fis.close();
			}
		}

		return count;
	}
	public static void setPrintStream(String fileString) throws FileNotFoundException {
		PrintStream ps = null;
		ps = new PrintStream(fileString);
		System.setOut(ps);

	}

	public static int searchFileName(String fileString, String regexString) throws Exception {
		File file = new File(fileString);
		fileString = "/home/dsw/log/search.log";
		if (file.isDirectory()) {
			File subFiles[] = file.listFiles();
			if (file.toString().matches(regexString) && !file.toString().contains(".")) {
				System.out.println(file);
			}
			for (File subFile : subFiles) {
				searchFileName(subFile.toString(), regexString);
			}
		} else {
			if (file.toString().matches(regexString) && !file.toString().contains(".")) {
				System.out.println(file);
			}
		}
		return 1;
	}

	public static int replace(int count, String fileString, String regexString) throws Exception {
		File file = new File(fileString);

		String logfileString = "D:\\log.txt";
		if (file.isDirectory()) {
			File subFiles[] = file.listFiles();
			for (File subFile : subFiles) {
				count = replace(count, subFile.toString(), regexString);
			}
		} else {
			if (file.toString().endsWith(".jsp")) {
				FileInputStream fis = new FileInputStream(file);
				BufferedInputStream bis = new BufferedInputStream(fis);
				InputStreamReader isr = new InputStreamReader(bis);
				BufferedReader br = new BufferedReader(isr);

				String str = null;
				ArrayList<String> store = new ArrayList<String>();
				int i = 0;
				boolean flag = true;
				boolean writeflag = false;
				while (null != (str = br.readLine())) {
					i++;
					Pattern pattern = Pattern.compile(regexString, Pattern.CASE_INSENSITIVE);
					Matcher matcher = pattern.matcher(str);
					if (matcher.matches()) {
						count++;
						if (flag) {
							System.out.println(file + "\r\n" + " : 第" + i + "行:\r\n" + str + "\r\n是否替换:\r\n");
							save(file + "\r\n" + " : 第" + i + "行:\r\n" + str + "\r\n是否替换:\r\n", logfileString);
							flag = false;
						} else {
							System.out.println("第" + i + "行:\r\n" + str + "\r\n是否替换:\r\n");
							save("第" + i + "行:\r\n" + str + "\r\n是否替换:\r\n", logfileString);
						}
						int c = System.in.read();
						while (c != 'y' && c != 'Y' && c != 'N' && c != 'n') {
							c = System.in.read();
						}
						if (c == 'Y' || c == 'y') {
							writeflag = true;
							store.add(matcher.group(1) + "aaa" + matcher.group(3));
						}
						if (c == 'n' || c == 'N') {
							writeflag = true;
							store.add(str);
						}
					} else {
						store.add(str);
					}
				}
				br.close();
				isr.close();
				bis.close();
				fis.close();
				if (true == writeflag) {
					file.delete();
					for (String str1 : store) {
						save(str1, fileString);
					}
				}
			}
		}
		return count;
	}

	public static void save(String str, String fileString) throws Exception {

		File file = new File(fileString);
		if (!file.exists()) {
			file.createNewFile();
		}

		FileOutputStream fos = new FileOutputStream(fileString, true);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		OutputStreamWriter osr = new OutputStreamWriter(bos);
		BufferedWriter bw = new BufferedWriter(osr);

		bw.write(str);
		bw.newLine();
		bw.flush();

	}

}
